package de.gwtprime.client.presenter;

import com.google.web.bindery.requestfactory.shared.Receiver;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.PrimeRequestFactory;
import de.gwtprime.client.event.VotesLoadedEvent;
import de.gwtprime.client.event.VotesSectionCompleteLoadedEvent;
import de.gwtprime.client.event.handler.VotesLoadedEventHandler;
import de.gwtprime.client.event.handler.VotesSectionCompleteLoadedEventHandler;
import de.gwtprime.client.widgets.ArticleSectionControlWidgetInterface;
import de.gwtprime.shared.VoteType;
import de.gwtprime.shared.requestFactory.ArticleSectionProxy;
import de.gwtprime.shared.requestFactory.VoteProxy;
import de.gwtprime.shared.requestFactory.VoteSectionCompletedProxy;
import de.gwtprime.shared.requestFactory.request.ArticleSectionRequest;
import de.gwtprime.shared.requestFactory.request.VoteRequest;

public class VoteControlPresenterArticleSectionImpl extends VoteControlPresenterAbstract {



	protected ArticleSectionProxy articleSection;

	public VoteControlPresenterArticleSectionImpl(ArticleSectionProxy articleSection,
			ClientFactory clientFactory, ArticleSectionControlWidgetInterface controlWidget){


		super(clientFactory, controlWidget, null);

		setVoteValue(articleSection.getVoteValue());
		
		this.articleSection = articleSection;
	}


	@Override
	public void voteUp() {

		super.voteUp();
		final PrimeRequestFactory requestFactory = clientFactory.getRequestFactory();
		VoteRequest request = requestFactory.voteRequest();
		VoteProxy vote = request.create(VoteProxy.class);
		vote.setType(VoteType.UP);

		request.addVote(articleSection.getId()).using(vote).fire(new Receiver<Void>() {

			@Override
			public void onSuccess(Void response) {
				System.out.println("success");// TODO Auto-generated method stub

			}
		});

	}

	@Override
	public void voteDown() {

		super.voteDown();

		final PrimeRequestFactory requestFactory = clientFactory.getRequestFactory();
		ArticleSectionRequest request = requestFactory.articleSectionRequest();

		VoteProxy vote = request.create(VoteProxy.class);
		vote.setType(VoteType.DOWN);

		request.addVoteAndPersist(vote, clientFactory.getUser()).using(articleSection).fire(new Receiver<Void>() {

			@Override
			public void onSuccess(Void response) {
				System.out.println("success");// TODO Auto-generated method stub

			}
		});

	}

	@Override
	public void initializeListeners() {
		clientFactory.getEventBus().addHandler(VotesLoadedEvent.TYPE, new VotesLoadedEventHandler() {

			@Override
			public void onVotesLoaded(VotesLoadedEvent event) {
				onVotesLoaded(articleSection, event);

			}

			private void onVotesLoaded(
					final ArticleSectionProxy articleSection,
					VotesLoadedEvent event) {
				
				for(VoteProxy vote : event.getVotes()){
					if(articleSection.getId().equals(vote.getTest())){

						// Disable vote buttons if vote is already applied
						voteType = vote.getType();
						if(vote.getType()==VoteType.DOWN){
							setVoteDownBtnActiveOnUI();
						}

						else if(vote.getType()==VoteType.UP){
							setVoteUpBtnActiveOnUI();

						}
						break;
					}
				}
			}
		});


		clientFactory.getEventBus().addHandler(VotesSectionCompleteLoadedEvent.TYPE, new VotesSectionCompleteLoadedEventHandler() {


			@Override
			public void onVotesLoaded(VotesSectionCompleteLoadedEvent event) {
				for(VoteSectionCompletedProxy vote : event.getVotes()){
					if(articleSection.getId().equals(vote.getTest())){
						setVoteCompleteBtnActiveOnUI();
						break;
					}
				}
			}
		});

	}




	@Override
	public void onVoteComplete() {

		super.onVoteComplete();

		final PrimeRequestFactory requestFactory = clientFactory.getRequestFactory();
		ArticleSectionRequest request = requestFactory.articleSectionRequest();

		VoteSectionCompletedProxy vote = request.create(VoteSectionCompletedProxy.class);

		request.addVoteSectionCompleteAndPersist(vote, clientFactory.getUser()).using(articleSection).fire(new Receiver<Void>() {

			@Override
			public void onSuccess(Void response) {
				System.out.println("success");// TODO Auto-generated method stub

			}
		});

	}


}
